// packageC/Myshaky/Myshaky/Myshaky.js
var app = getApp();
import util from '../../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istrue: [{
      id: 0,
      tit: '全部'
    },
    {
      id: 1,
      tit: '待付款'
    },
    {
      id: 2,
      tit: '报名成功'
    }

    ],
    curr: 0,
    page: 1,
    id: wx.getStorageSync('yz_uid')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  goBmxq(e){
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '/packageC/Myshaky/RegistrationDetail/RegistrationDetail?id=' + e.currentTarget.id,
    });
  },
  toRefund(e){
    let id = e.currentTarget.id;
    let item  = e.currentTarget.dataset.item;
    console.log(item);
    if(item.refund_id){
      // 退款中
      wx.navigateTo({
        url: "/packageD/member/myOrder/Aftersales/Aftersales?refund_id=" + item.refund_id,
      });
    }else{
      wx.navigateTo({
        url: "/packageD/member/myOrder/refund/refund?order_id=" + id + "&type=1",
      });
    }
  },
  getLine(e) {
    console.log(e.target.id);
    console.log(this.data.page);
    this.setData({
      curr: e.target.id,
      page:1
    });
    this.getdata(this.data.curr);
  },
  toPay(e) {
    console.log(e.currentTarget.id);
    var pid = e.currentTarget.id;
    wx.navigateTo({
      url: '/packageD/buy/orderPay/orderPay?order_id=' + pid +'&name=Myshaky',
    });
  },
  cancleOrder(e) {
    console.log(e.currentTarget.id);
    var kid = e.currentTarget.id;
    // let that = this;
    wx.showModal({
      title: '提示',
      content: '确定取消此订单?',
      success:(res)=> {
        if (res.confirm) {
          console.log('用户点击确定');
          let urlStr = app.getNetAddresss("order.operation.close");
          app._getNetWork({
            url: urlStr,
            data: {
              order_id: kid
            },
            success: (resdata)=> {
              this.getdata(this.data.curr);
            },
            fail: function (res) {
              console.log(res);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getdata(this.data.curr);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // console.log(that.data.current_page);
    if(that.data.current_page < that.data.last_page){
      wx.showLoading({
        title: '加载中',
        mask: true,
      });
      that.setData({
        page:that.data.page+1
      });
      console.log(that.data.page);
      that.getNext();
    }else{
      wx.showToast({
        title: '没有更多',
        icon: 'none'
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getdata(id) {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.lecturer.getOrder");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        member_id: that.data.id,
        page: that.data.page,
        status: id
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          let lis = res.data.data;
          for (let i = 0; i < lis.length; i++) {
            lis[i].start_time = util.formatTimeTwo(lis[i].start_time, 'Y/M/D h:m:s');
          }
          that.setData({
            datas: lis,
            current_page: res.data.current_page,
            last_page:res.data.last_page
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getNext() {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.lecturer.getOrder");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        member_id: that.data.id,
        page: that.data.page,
        status: that.data.curr
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          let lis = res.data.data;
          for (let i = 0; i < lis.length; i++) {
            lis[i].start_time = util.formatTimeTwo(lis[i].start_time, 'Y/M/D h:m:s');
          }
          if(res.data.current_page > res.data.last_page){
            wx.hideLoading();
            wx.showToast({
              title: '没有更多',
              icon: 'none'
            });
            that.setData({
              current_page: res.data.current_page,
              last_page:res.data.last_page
            });
            return;
          }else{
            wx.hideLoading();
            that.setData({
              datas:that.data.datas.concat(lis)
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});